package com.common.www.utils;

import java.util.Calendar;

/**
 * Created by cubebox on 2017/12/26.
 */

public class RecentTimeUtils {
    /**
     * 格式化时间。刚刚、x分钟前、x小时前、x天前、x月前、x年前
     *
     * @param time 毫秒 需要计算你的时间戳
     */
    public static String formatBeforeTime(long time) {
        if (time <= 0)
            return "未知";

        String res = "";

        long tmp = (System.currentTimeMillis() - time) / 1000;

        long year = tmp / (3600 * 24 * 30 * 12);
        long month = tmp / (3600 * 24 * 30);
        long day = tmp / (3600 * 24);
        long hour = tmp / 3600;
        long minute = tmp / 60;
        long secound = tmp % 60;
        if (year > 0) {
            res = year + "年前";
        } else if (month > 0) {
            res = month + "月前";
        } else if (day > 0) {
            res = day + "天前";
        } else if (hour > 0) {
            res = hour + "小时前";
        } else if (minute > 0) {
            res = minute + "分钟前";
        } else {
            res = "刚刚";
        }

        return res;
    }

    /**
     * 格式化时间。刚刚、x分钟前、x小时前、昨天、前天 、星期x、x天前、显示具体日期
     *
     * @param time 毫秒 需要计算你的时间戳
     */
    public static String formatBeforeTime2(long time) {
        if (time <= 0)
            return "未知";

        String res = "";
        //获取今天凌晨和昨天凌晨的时间戳单独判断
        Calendar today = Calendar.getInstance();//今天凌晨
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        Calendar yesterday = Calendar.getInstance();//昨天凌晨
        yesterday.add(Calendar.DAY_OF_MONTH, -1);
        yesterday.set(Calendar.HOUR_OF_DAY, 0);
        yesterday.set(Calendar.MINUTE, 0);
        yesterday.set(Calendar.SECOND, 0);
        yesterday.set(Calendar.MILLISECOND, 0);

        Calendar lastYesterday = Calendar.getInstance();//前天凌晨
        lastYesterday.add(Calendar.DAY_OF_MONTH, -2);
        lastYesterday.set(Calendar.HOUR_OF_DAY, 0);
        lastYesterday.set(Calendar.MINUTE, 0);
        lastYesterday.set(Calendar.SECOND, 0);
        lastYesterday.set(Calendar.MILLISECOND, 0);

        Calendar day5 = Calendar.getInstance();//前5天
        day5.add(Calendar.DAY_OF_MONTH, -5);
        day5.set(Calendar.HOUR_OF_DAY, 0);
        day5.set(Calendar.MINUTE, 0);
        day5.set(Calendar.SECOND, 0);
        day5.set(Calendar.MILLISECOND, 0);

        long tmp = (System.currentTimeMillis() - time) / 1000;
        if (time >= today.getTimeInMillis()) {//今天内的时间
            long hour = tmp / 3600;
            long minute = tmp / 60;
            long secound = tmp % 60;

            if (hour > 0) {
//                res = hour + "小时前";
                res = "今天" + TimeUtils.getTimeStringByTimetamp(time, TimeUtils.HM);
            } else if (minute > 0) {
                res = minute + "分钟前";
            } else {
                res = "刚刚";
            }
            return res;
        } else if (time < today.getTimeInMillis() && time >= yesterday.getTimeInMillis()) {//昨天的时间
            return "昨天" + TimeUtils.getTimeStringByTimetamp(time, TimeUtils.HM);
        } else if (time < yesterday.getTimeInMillis() && time >= lastYesterday.getTimeInMillis()) {//前天的时间范围
            return "前天" + TimeUtils.getTimeStringByTimetamp(time, TimeUtils.HM);
        } else if (time < lastYesterday.getTimeInMillis() && time >= day5.getTimeInMillis()) {//进5天内的显示星期数
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            return "星期" + NumChineseUtils.numberToChinese(calendar.get(Calendar.DAY_OF_WEEK) - 1);
        } else {//其他的显示具体的日期即可
            return TimeUtils.getTimeStringByTimetamp(time);
        }
    }

    /**
     * 格式化时间。今天 明天 后天  xxx月xx日
     *
     * @param time 毫秒 需要计算你的时间戳
     */
    public static String formatAfterTime(long time) {
        if (time <= 0)
            return "未知";

        String res = "";
        //获取今天凌晨和昨天凌晨的时间戳单独判断
        Calendar today = Calendar.getInstance();//今天凌晨
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        Calendar tomorrow = Calendar.getInstance();//明天凌晨
        tomorrow.add(Calendar.DAY_OF_MONTH, 1);
        tomorrow.set(Calendar.HOUR_OF_DAY, 0);
        tomorrow.set(Calendar.MINUTE, 0);
        tomorrow.set(Calendar.SECOND, 0);
        tomorrow.set(Calendar.MILLISECOND, 0);

        Calendar theDayAfTomorrow = Calendar.getInstance();//后天凌晨
        theDayAfTomorrow.add(Calendar.DAY_OF_MONTH, 2);
        theDayAfTomorrow.set(Calendar.HOUR_OF_DAY, 0);
        theDayAfTomorrow.set(Calendar.MINUTE, 0);
        theDayAfTomorrow.set(Calendar.SECOND, 0);
        theDayAfTomorrow.set(Calendar.MILLISECOND, 0);

        Calendar theDayAfTomorrow2 = Calendar.getInstance();//后天凌晨
        theDayAfTomorrow2.add(Calendar.DAY_OF_MONTH, 3);
        theDayAfTomorrow2.set(Calendar.HOUR_OF_DAY, 0);
        theDayAfTomorrow2.set(Calendar.MINUTE, 0);
        theDayAfTomorrow2.set(Calendar.SECOND, 0);
        theDayAfTomorrow2.set(Calendar.MILLISECOND, 0);


        long tmp = (System.currentTimeMillis() - time) / 1000;
        if (time >= today.getTimeInMillis() && time < tomorrow.getTimeInMillis()) {//今天内的时间
            res = "今天" + TimeUtils.getTimeStringByTimetamp(time, TimeUtils.HM);
            return res;
        } else if (time >= tomorrow.getTimeInMillis() && time < theDayAfTomorrow.getTimeInMillis()) {//昨天的时间
            return "明天" + TimeUtils.getTimeStringByTimetamp(time, TimeUtils.HM);
        } else if (time >= theDayAfTomorrow.getTimeInMillis() && time < theDayAfTomorrow2.getTimeInMillis()) {//前天的时间范围
            return "后天" + TimeUtils.getTimeStringByTimetamp(time, TimeUtils.HM);
        } else {//其他的显示具体的日期即可
            return TimeUtils.getTimeStringByTimetamp(time);
        }
    }

    /**
     * 精确格式化时间。刚刚、x分钟前、x小时前、x天前、x月前、x年前
     *
     * @param time 毫秒 需要计算你的时间戳
     */
    public static String formatBeforeTimeExact(long time) {
        if (time <= 0)
            return "未知";

        String res = "";
        long tmp = (System.currentTimeMillis() - time) / 1000;

        long year = tmp / (3600 * 24 * 30 * 12);
        long month = tmp / (3600 * 24 * 30);
        long day = tmp / (3600 * 24);
        long hour = tmp / 3600;
        long minute = tmp / 60;
        long secound = tmp % 60;
        if (day > 0) {// 超过一天后就显示全部时间
            res = TimeUtils.getTimeStringByTimetamp(tmp * 1000);
        } else if (hour > 0) {
            res = hour + "小时" + ((minute - (hour * 60)) > 0 ? ((minute - (hour * 60)) + "分") : "") + "前";
        } else if (minute > 0) {
            res = minute + "分" + (secound > 0 ? (secound + "秒") : "钟") + "前";
        } else {
            res = secound + "秒前";
        }

        return res;
    }

    /**
     * 格式化时间。刚刚、x分钟前、x小时前、x天前、x月前、x年前
     *
     * @param time 毫秒
     */
    public static String formatBeforeTime(String time) {
        return formatBeforeTime(TxtUtil.getLong(time));
    }

    /**
     * 格式化时间。刚刚、x分钟前、x小时前、x天前、x月前、x年前
     *
     * @param time 毫秒
     */
    public static String formatBeforeTimeExact(String time) {
        return formatBeforeTimeExact(TxtUtil.getLong(time));
    }

    public static void main(String[] arg) {
//        long time = 10 * 60 * 60 * 1000;
//        for (long i = 0; i < 50 * time; i += time) {
//            String format = formatBeforeTime2(System.currentTimeMillis() - i);
//            System.out.println(format);
//        }
//        String format = formatBeforeTime2(1585682289000L);
//        System.out.println(format);
    }
}
